package com.kenliang.javase.concurrent.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author KenLiang
 * @description:Lock，Condition类的使用
 * @date Created in 5:35 PM 2019/3/13.
 */
public class LockService {

    private Lock lock =  new ReentrantLock(false);



    public void methodA(){
        try {
            lock.lock();
            for(int i=0;i<10;i++){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //这个condition 最好还是只用与一个条件
        LockService conditionWaitNotifyService = new LockService();
        Thread t1 = new Thread(conditionWaitNotifyService::methodA);
        t1.setName("t1");

        Thread t2 = new Thread(conditionWaitNotifyService::methodA);
        t2.setName("t2");

        Thread t3 = new Thread(conditionWaitNotifyService::methodA);
        t3.setName("t3");

        LockService conditionWaitNotifyService1 = new LockService();
        Thread t4 = new Thread(conditionWaitNotifyService1::methodA);
        t4.setName("t4");

        t4.start();
        t1.start();
        t2.start();
        t3.start();
    }

}
